#!/bin/bash
#
# file-latch.sh
#
# Usage:
#   ...

set -o nounset

die() {
  echo 1>&2 "$@"
  exit 1
}

main() {
  which inotifywait \
    || die "inotifywait must be installed (apt-get install inotifytools)."

  local filename=$1
  while true; do
    echo Watching "$@"
    inotifywait --event modify "$@"
    local exit_code=$?
    if test $? -ne 0; then
      die "inotifywait failed with code $exit_code"
    fi
    # Add latch automatically.
    PULP_latch=1 poly build .
    curl -d X http://localhost:1212/HOST/latch/default
    echo
  done
}

main "$@"
